from flask import Blueprint,request,jsonify
from db import db1
from comm import get_data
bp_course=Blueprint('course',__name__)
@bp_course.route('/Bumen',methods=['GET','POST','PUT','DELETE'])
def Bumen():
    if request.method=='POST':
        data=get_data()
        sql="insert into bumen values(0,'%s',%d)"%(data['name'],int(data['total']))
        db1.insert(sql)
        return jsonify({'code':200})

    elif request.method=='GET':
        id=request.args.get('id')
        if id:
            sql="select * from bumen where id=%d"%(int(id))
            res=db1.find(sql)
            return jsonify({'data':res})
        else:
            sql="select * from bumen"
            res=db1.find(sql)
            return jsonify({"data":res})
    elif request.method=='PUT':
        data=request.form
        sql="update bumen set name='%s'"%(data['name'])
        db1.insert(sql)
        return jsonify({"code":200})
    elif request.method=='DELETE':
        id=request.args.get('id')
        sql="delete from bumen where id=%d"%(int(id))
        db1.insert(sql)
        return jsonify({"code":200})

        
@bp_course.route('/yuangon',methods=['GET','POST','PUT','DELETE'])
def yuangon():
    if request.method=='POST':
        data=get_data()
        sql="insert into yuangon values(0,'%s','%s',%d,%d)"%(data['name'],data['gender'],int(data['money']),int(data['bm']))
        db1.insert(sql)
        return jsonify({'code':200})

    elif request.method=='GET':
        id=request.args.get('id')
        if id:
            sql="select * from yuangon where id=%d"%(int(id))
            res=db1.find(sql)
            return jsonify({'data':res})
        else:
            sql="select * from yuangon"
            res=db1.find(sql)
            return jsonify({"data":res})
    elif request.method=='PUT':
        data=request.form
        sql="update yuangon set name='%s'"%(data['name'])
        db1.insert(sql)
        return jsonify({"code":200})
    elif request.method=='DELETE':
        id=request.args.get('id')
        sql="delete from yuangon where id=%d"%(int(id))
        db1.insert(sql)
        return jsonify({"code":200})

